package com.liuf.di.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuf.di.api.ServiceException;
import com.liuf.di.configuration.kettle.KettleInitFactory;
import com.liuf.di.repository.ITranRepository;
import com.liuf.di.service.ITranService;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.repository.LongObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @program: spring-cloud-liuf-master
 * @description: 转换业务管理实现
 * @author: Mr.Liu
 * @create: 2018-09-20 16:21
 * @码云： https://gitee.com/www.jiandan.com
 **/
@Service
public class TranServiceImpl implements ITranService {

    private static final Logger logger = LoggerFactory.getLogger(TranServiceImpl.class);

    /**资源库*/
    private Repository repository = null;

    @Autowired
    private KettleInitFactory kettleInitFactory = null;

    private TransMeta transMeta = null;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ITranRepository tranRepository;

    @Override
    public PageInfo findTrans(Map<String, Object> params) throws ServiceException {
        PageHelper.startPage((Integer) params.get("pageNum"),(Integer) params.get("pageSize"));
        return new PageInfo(tranRepository.findTrans(params));
    }

    @Override
    public Map<String, Object> getTran(long id) throws ServiceException {
        return tranRepository.getTran(id);
    }

    @Override
    public TransMeta loadTran(String tranName, long directory) throws ServiceException {
        repository = kettleInitFactory.getRepositoryInit();
        transMeta = new TransMeta();
        try {
            transMeta = repository.loadTransformation(new LongObjectId(directory), null);
            return transMeta;
        } catch (KettleException e) {
            e.printStackTrace();
        }finally {
            /**关闭资源库连接*/
            repository.disconnect();
        }
        return null;
    }

    @Override
    public void delTran(long id) throws ServiceException {
        repository = kettleInitFactory.getRepositoryInit();
        try {
            repository.deleteTransformation(new LongObjectId(id));
        } catch (KettleException e) {
            e.printStackTrace();
            logger.error("转换删除失败="+e.getMessage());
        }finally {
            /**关闭资源库连接*/
            repository.disconnect();
        }
    }

    @Override
    public void TranExecution(TransMeta transMeta) throws ServiceException {
        repository = kettleInitFactory.getRepositoryInit();
        Trans trans = new Trans(transMeta);
        try {
            trans.execute(null);
            trans.waitUntilFinished();
        } catch (KettleException e) {
            e.printStackTrace();
        }finally {
            repository.disconnect();
        }
    }
}
